package com.vincent.common.utils;

import com.vincent.common.enums.FormSubmitFlag;
import com.vincent.common.enums.ResultMapKey;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * 返回结果处理工具类
 */
public class ResultMapUtil {

    /**
     * ajax请求中返回处理信息，没有数据
     * @param serviceMap 从service中返回的结果集合
     * @param resultCodeMessageSource 结果码信息对象
     * @return
     */
    public static Map<String, Object> getAjaxMap(Map<String, Object> serviceMap, ReloadableResourceBundleMessageSource resultCodeMessageSource) {
        Map<String, Object> resultmap = new HashMap<String, Object>();
        String errorKey = ResultMapKey.ERROR.getCode();
        Object errorObj = serviceMap.get(errorKey);
        Locale locale = Locale.getDefault();
        if (errorObj != null) {
            String message = resultCodeMessageSource.getMessage(errorObj.toString(), null, locale);
            resultmap.put(errorKey, message);
        } else {
            String successKey = ResultMapKey.SUCCESS.getCode();
            Object successObj = serviceMap.get(successKey);
            String message = resultCodeMessageSource.getMessage(successObj.toString(), null, locale);
            resultmap.put(successKey, message);
        }
        return resultmap;
    }

    /**
     * <p>ajax请求中返回处理信息，没有数据</p>
     *<p>表单提交，是否只是提交一次</p>
     * @param serviceMap 从service中返回的结果集合
     * @param resultCodeMessageSource 结果码信息对象
     * @return
     */
    public static Map<String, Object> getAjaxPostOnceMap(Map<String, Object> serviceMap, ReloadableResourceBundleMessageSource resultCodeMessageSource) {
        Map<String, Object> resultmap = new HashMap<String, Object>();
        String errorKey = ResultMapKey.ERROR.getCode();
        Object errorObj = serviceMap.get(errorKey);
        Locale locale = Locale.getDefault();
        if (errorObj != null) {
            String message = resultCodeMessageSource.getMessage(errorObj.toString(), null, locale);
            resultmap.put(errorKey, message);
            resultmap.put("status", FormSubmitFlag.CONTINUE.getCode());
        } else {
            String successKey = ResultMapKey.SUCCESS.getCode();
            Object successObj = serviceMap.get(successKey);
            String message = resultCodeMessageSource.getMessage(successObj.toString(), null, locale);
            resultmap.put(successKey, message);
            resultmap.put("status", FormSubmitFlag.FORBIDDEN.getCode());
        }
        return resultmap;
    }

    /**
     * ajax请求，表单提交限制，返回数据既有结果码也有不需要传到前台的对象
     * @param serviceMap
     * @param resultCodeMessageSource
     * @return
     */
    public static Map<String, Object> getAjaxPostOnceDataMap(Map<String, Object> serviceMap, ReloadableResourceBundleMessageSource resultCodeMessageSource) {
        Map<String,Object> map=new HashMap<String, Object>();
        Map<String, Object> resultmap = new HashMap<String, Object>();
        String errorKey = ResultMapKey.ERROR.getCode();
        Object errorObj = serviceMap.get(errorKey);
        Locale locale = Locale.getDefault();
        if (errorObj != null) {
            String message = resultCodeMessageSource.getMessage(errorObj.toString(), null, locale);
            resultmap.put(errorKey, message);
            resultmap.put("status", FormSubmitFlag.CONTINUE.getCode());
        } else {
            String successKey = ResultMapKey.SUCCESS.getCode();
            Object successObj = serviceMap.get(successKey);
            Map<String,Object> successMap= (Map<String, Object>) successObj;
            Object data = successMap.get(ResultMapKey.DATA.getCode());
            map.put(ResultMapKey.DATA.getCode(),data);
            Object resultCodeObj = successMap.get(ResultMapKey.RESULTCODE.getCode());
            String message = resultCodeMessageSource.getMessage(resultCodeObj.toString(), null, locale);
            resultmap.put(successKey, message);
            resultmap.put("status", FormSubmitFlag.FORBIDDEN.getCode());
        }
        map.put(ResultMapKey.RESULTMAP.getCode(),resultmap);
        return map;
    }

    public static Object getSuccessObj(Map<String, Object> serviceMap){
        String successKey = ResultMapKey.SUCCESS.getCode();
        Object successObj = serviceMap.get(successKey);
        return successObj;
    }
    public static Object getErrorObj(Map<String, Object> serviceMap){
        String errorKey = ResultMapKey.ERROR.getCode();
        Object errorObj = serviceMap.get(errorKey);
        return errorObj;
    }
}
